//
//  AFAppClient.m
//  AFAppClient
//
//  Created by Irewest on 14-8-4.
//  Copyright (c) 2014年 Irewest. All rights reserved.
//

#import "AFAppClient.h"

@implementation AFAppClient

+ (AFAppClient *)sharedManager
{
    static dispatch_once_t once;
	static AFAppClient *_sharedManager;
	dispatch_once(&once,^{
		_sharedManager = [[AFAppClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.qubaobei.com/ios/api/"]];
	});
	return _sharedManager;
}

/**
 * POST 数据
 * @urlString 请求链接
 * @paramters post传递数据
 * @successBlock 请求成功回调
 * @failureBlock 请求失败回调
 */
- (void)postWithURL:(NSString *)urlString
     withParameters:(NSDictionary *)paramters
            success:(void (^)(id responseObject))successBlock
            failure:(void (^)(NSError *error))failureBlock
{
    debugLog(@"%@",urlString);
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    
    NSMutableDictionary *textParams = [[NSMutableDictionary alloc] init];
    NSMutableDictionary *imageParams = [[NSMutableDictionary alloc] init];
    NSArray *keys = [paramters allKeys];
    for (NSString *key in keys) {
        if ([paramters[key] isKindOfClass:[UIImage class]]) {
            [imageParams setObject:paramters[key] forKey:key];
        }else {
            [textParams setObject:paramters[key] forKey:key];
        }
    }

    if (imageParams && [imageParams count] > 0) {
        [self POST:urlString parameters:textParams constructingBodyWithBlock:^(id <AFMultipartFormData> formData){
            CGFloat compression = 1.0f;
            CGFloat maxCompression = 0.1f;
            int maxFileSize = 250*1024;
            NSArray *keys = [imageParams allKeys];
            NSUInteger i=0;
            for (NSString *key in keys) {
                NSData *imageData = UIImageJPEGRepresentation(imageParams[key], compression);
                while ([imageData length] > maxFileSize && compression > maxCompression)
                {
                    compression -= 0.1;
                    imageData = UIImageJPEGRepresentation(imageParams[key], compression);
                }
                [formData appendPartWithFileData:imageData
                                            name:[NSString stringWithFormat:@"pic%ld",i]
                                        fileName:key
                                        mimeType:@"image/jpeg"];
                i++;
            }
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (successBlock) {
                successBlock((NSDictionary *)responseObject);
            };
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            if (failureBlock) {
                failureBlock(error);
            };
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        }];
    }else {
        [self POST:urlString parameters:paramters success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if (successBlock) {
                successBlock(responseObject);
            };
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            if (failureBlock) {
                failureBlock(error);
            };
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        }];
    }
}

/**
 * GET 普通数据
 * @urlString 请求链接
 * @paramters get传递数据
 * @successBlock 请求成功回调
 * @failureBlock 请求失败回调
 */
- (void)getWithURL:(NSString *)urlString
    withParameters:(NSDictionary *)paramters
           success:(void (^)(id responseObject))successBlock
           failure:(void (^)(NSError *error))failureBlock
{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json",nil];
    //self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    [self GET:urlString parameters:paramters success:^(AFHTTPRequestOperation *operation, id responseObject) {
		if (successBlock) {
			successBlock((NSDictionary *)responseObject);
		};
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
	} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
		if (failureBlock) {
			failureBlock(error);
		};
        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
	}];
}

@end
